<?php

if (!function_exists('root_path')) {
    function root_path($file)
    {
        $path = getcwd();
        return "{$path}/{$file}";
    }
}

if (!function_exists('storage_path')) {
    function storage_path($file)
    {
        $file = "storage/{$file}";
        return root_path($file);
    }
}

if (!function_exists('get_millisecond')) {
    function get_millisecond()
    {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000)/1000;
    }
}

if (!function_exists('yqok')) {
    function yqok($params = [])
    {
        return yqresponse(0, 'success', $params);
    }
}

if (!function_exists('yqerr')) {
    function yqerr($code, $msg = '')
    {
        $msg = trans($code);
        return yqresponse($code, $msg);
    }
}

if (!function_exists('yqresponse')) {
   function yqresponse(int $code, string $msg, array $params = [])
   {
       $content = [
           'code' => $code,
           'msg'  => $msg,
           'data' => $params,
           'time' => time(),
       ];

       return response(json_encode($content, JSON_UNESCAPED_UNICODE), 200)
                   ->header('Content-Type', 'application/json;charset=UTF-8');
   }
}

if (!function_exists('yqlog')) {
    /**
     * 打印日志
     * @return void
     */
    function yqlog()
    {
        $args     = func_num_args();
        $arg_list = func_get_args();
        if ($args <= 0) {
            return;
        }

        $array = debug_backtrace();
        $row   = $array[0];
        $msg   = "[RUNTIME] " . $row['file'] . ':' . $row['line'] . "\t\t";
        foreach ($arg_list as $value) {
            if (is_array($value)) {
                $msg = $msg . json_encode($value, JSON_UNESCAPED_UNICODE) . "\t";
            } else {
                $msg = $msg . $value . "\t";
            }
        }
        \support\Log::info($msg);
    }
}
